package com.mobile_fault_detector.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mobile_fault_detector.entity.RepairTicket;
import com.mobile_fault_detector.mapper.IRepairTicketMapper;
import com.mobile_fault_detector.service.IRepairTicketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工单服务实现类
 */
@Slf4j
@Service
public class RepairTicketServiceImpl implements IRepairTicketService {

    private final IRepairTicketMapper repairTicketMapper;

    @Autowired
    public RepairTicketServiceImpl(IRepairTicketMapper repairTicketMapper) {
        this.repairTicketMapper = repairTicketMapper;
    }

    @Override
    public RepairTicket createTicket(RepairTicket repairTicket) {
        log.info("创建工单: {}", repairTicket);
        repairTicketMapper.insert(repairTicket);
        return repairTicket;
    }

    @Override
    public RepairTicket getTicketById(Long ticketId) {
        log.info("根据ID查询工单信息: {}", ticketId);
        return repairTicketMapper.selectById(ticketId);
    }

    @Override
    public List<RepairTicket> getTicketsByUserId(Long userId) {
        log.info("根据用户ID查询工单列表: {}", userId);
        LambdaQueryWrapper<RepairTicket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RepairTicket::getUserId, userId);
        return repairTicketMapper.selectList(queryWrapper);
    }

    @Override
    public RepairTicket updateTicketStatus(Long ticketId, Integer status) {
        log.info("更新工单状态, 工单ID: {}, 状态: {}", ticketId, status);
        RepairTicket repairTicket = repairTicketMapper.selectById(ticketId);
        if (repairTicket != null) {
            repairTicket.setTicketStatus(status);
            repairTicketMapper.updateById(repairTicket);
        }
        return repairTicket;
    }
}